//
//  PLCAudioPath.m
//  PLCAudioRecorderDemo
//
//  Created by PlutusCat on 2017/4/27.
//  Copyright © 2017年 PlutusCat. All rights reserved.
//

#import "PLCAudioPath.h"

@implementation PLCAudioPath
/**
 *  取得录音文件保存路径
 *
 *  @return 录音文件路径
 */
+ (NSURL *)getRecordRootDirPath {
    NSURL * url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@record_flutter",NSTemporaryDirectory()]];
    BOOL dir = YES;
    if (![[NSFileManager defaultManager] fileExistsAtPath:url.path isDirectory:&dir]){
        [[NSFileManager defaultManager] createDirectoryAtURL:url withIntermediateDirectories:nil attributes:nil error:nil];
    }
    return url;
}

+ (NSURL *)genernalRecordFilePath {
    NSString *fileName = [[[[[NSUUID UUID] UUIDString] stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString] substringToIndex:10];
    return [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.caf", [self getRecordRootDirPath].path, fileName]];
}


+ (NSURL *)getMp3RecordFileWithPath:(NSString *)path {
    NSString *dir = [NSString stringWithFormat:@"%@.mp3",[path stringByDeletingPathExtension]];
    return [NSURL fileURLWithPath:dir];
}

+(void)deleteInvalidRecordFile{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [[fileManager contentsOfDirectoryAtURL:[self getRecordRootDirPath] includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:nil] enumerateObjectsUsingBlock:^(NSURL * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj){
           NSDictionary *attrs = [fileManager attributesOfItemAtPath:obj.path error:nil];
           NSDate *date = attrs[NSFileCreationDate];
            if ([[NSDate date] timeIntervalSinceDate:date] > 3600*24){
                [fileManager removeItemAtURL:obj error:nil];
                NSLog(@"已删除：%@", obj.path);
            }
        }
    }];
}

+ (void)deleteRecordFile {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [[fileManager contentsOfDirectoryAtURL:[self getRecordRootDirPath] includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:nil] enumerateObjectsUsingBlock:^(NSURL * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj){
            [fileManager removeItemAtURL:obj error:nil];
        }
    }];
}

+(NSInteger)getFileSizeWithPath:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:path]){
        return 0;
    }
    NSDictionary *attrs = [fileManager attributesOfItemAtPath:path error:nil];
    NSInteger size = [attrs[NSFileSize] integerValue];
    return size;
}

+ (void)getRecordAttributesWithPath:(NSString *)path {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
    NSArray *keys;
    id key, value;
    keys = [fileAttributes allKeys];
    NSInteger count = [keys count];
    for (int i = 0; i < count; i++) {
        key = [keys objectAtIndex: i];  //获取文件名
        value = [fileAttributes objectForKey: key];  //获取文件属性
        
        NSLog(@"key = %@  value = %@",key, value);
    }
}
@end
